#! /usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage; # use POD for Usage

my %options=(
		os=>undef,
		nwmodsap=>undef,
		nwmodnmda=>undef,
		nwclient=>undef,
		path_inst=>'/home/dci6306',
		logfile=>'/nsr/upgrade.log',
		max=>undef,
		noclean=>undef,
		flag_help=>undef		
		);
		
GetOptions("inst=s"=>\$options{path_inst},
			"max"=>\$options{max},
			"noclean"=>\$options{noclean},
			"help|?"=>\$options{flag_help}
			);		

&pod2usage(verbose=>2) if ($options{flag_help}); # display usage info if help flag is defined

sub logging {
 	my $log_message = join(' ',@_); 
 	open (LOG,">>$options{logfile}") or die "could not open log file $options{logfile}\n"; # open log-file
 	print LOG localtime()." ".$log_message."\n"; # print message with timestamp into log-file
 	close (LOG) or die "could not close log file $options{logfile}\n"; # close log-file
}

sub determ_os(){
	if ($^O =~ /aix/){&logging("AIX OS detected - going forward")}
	else {&logging("not AIX -> exiting");exit 1;}
}
######    MAIN    #########
&logging ("-" x 40);
&determ_os;
my $found="no";
for my $line (`ps -ef`) {	#get a list of all running processes
 	chomp($line);				
 	if ($line =~ "backint" or $line =~ "brbackup" or $line =~ "brarchive" or $line =~ "brrecover" or $line =~ "brrestore" or $line =~ "savepnpc" or $line =~ "save" or $line =~ "nsrnmdb2" or $line =~ "nsrdasv" or $line =~ "nsrdb2" or $line =~ "recover"){ 
 		$found="yes";
 		&logging($line);
 		print $line, "\n";
 	}
}						#looking for these specified processes - if found print them 
if ($found eq "yes"){
	print "Backup or Restore Operation in progress - no update will be done!\n";
	&logging("Backup or Restore Operation in progress - no update will be done!")
}  #this is the end if one of the above processes will be found - nothing will be done
 
if ($found eq "no"){		#none of the processes was found running -> here we go
	&logging(`lslpp -L | grep LGTO`);
 	for my $lslpp (`lslpp -L | grep LGTO`){	#looking for installed LGTO Software
 		chomp($lslpp);
 		if ($lslpp =~ "LGTOnw.clnt.rte"){$options{nwclient}="LGTOnw.clnt.rte";}	#save NWClient if found
 		if ($lslpp =~ "LGTOnmsap.rte"){$options{nwmodsap}="LGTOnmsap.rte";}  	#save NMSAP if found
 		if ($lslpp =~ "LGTOnmda.rte"){$options{nwmodnmda}="LGTOnmda.rte";}	#save NMDA if found
 		
 	}


if ($options{nwclient}){
	&logging("nsr_shutdown");
	&logging(`nsr_shutdown`);
	&logging("rm -r /nsr/res/nsrladb /nsr/applogs /nsr/apps /nsr/cores /nsr/dedup /nsr/logs /nsr/tmp /nsr/run");
	&logging(`rm -r /nsr/res/nsrladb /nsr/applogs /nsr/apps /nsr/cores /nsr/dedup /nsr/logs /nsr/tmp /nsr/run`);
	if ($options{nwmodnmda}){
		&logging("installp -u $options{nwmodnmda}");
		&logging(`installp -u $options{nwmodnmda}`);
	}
	if ($options{nwmodsap}){
		&logging("installp -u $options{nwmodsap}");
		&logging(`installp -u $options{nwmodsap}`);
	}
	&logging("rm $options{path_inst}/.toc");
	&logging(`rm $options{path_inst}/.toc`);
	&logging("installp -u $options{nwclient}");
	&logging(`installp -u $options{nwclient}`);
	&logging("installp -a -d $options{path_inst} $options{nwclient}");
	&logging(`installp -a -d $options{path_inst} $options{nwclient}`);
	&logging('echo "/bin/chmod 777 /nsr/applogs" >> /etc/rc.nsr');
	&logging(`echo "/bin/chmod 777 /nsr/applogs" >> /etc/rc.nsr`);
	if ($options{max}){
		&logging("/bin/mkdir /nsr/pipe");
		&logging(`/bin/mkdir /nsr/pipe`);
		&logging("/bin/chmod 777 /nsr/pipe");
		&logging(`/bin/chmod 777 /nsr/pipe`);
		&logging('echo "/bin/chmod 775 /nsr/res" >> /etc/rc.nsr');
		&logging(`echo "/bin/chmod 775 /nsr/res" >> /etc/rc.nsr`);
		&logging('echo "/bin/chmod 775 /nsr/tmp" >> /etc/rc.nsr');
		&logging(`echo "/bin/chmod 775 /nsr/tmp" >> /etc/rc.nsr`);
		&logging("/bin/chgrp sapsys /nsr/res");
		&logging(`/bin/chgrp sapsys /nsr/res`);
		&logging("/bin/chgrp sapsys /nsr/tmp");
		&logging(`/bin/chgrp sapsys /nsr/tmp`);
	}
	&logging("/etc/rc.nsr");
	&logging(`/etc/rc.nsr`);
	&logging("nsr_shutdown");
	&logging(`nsr_shutdown`);
	&logging("rm $options{path_inst}/.toc");
	&logging(`rm $options{path_inst}/.toc`);
	if ($options{nwmodsap}){
		&logging("installp -a -d $options{path_inst} $options{nwmodsap}");
		&logging(`installp -a -d $options{path_inst} $options{nwmodsap}`);
		&logging("cp /home/dci6306/backint /usr/bin/backint");
		&logging(`cp /home/dci6306/backint /usr/bin/backint`);
		&logging("/bin/chown root:system /usr/bin/backint");
		&logging(`/bin/chown root:system /usr/bin/backint`);
		&logging("/bin/chmod 4755 /usr/bin/backint");
		&logging(`/bin/chmod 4755 /usr/bin/backint`);
		&logging(`ls -latr /usr/bin/backint`);
	}
	if ($options{nwmodnmda}){&logging(`installp -a -d $options{path_inst} $options{nwmodnmda}`);}
	&logging("/etc/rc.nsr");
	&logging(`/etc/rc.nsr`);
	&logging(`ps -ef | grep nsr`);
	&logging(`lslpp -L | grep LGTO`);
	&logging(`ls -latr /usr/sap/*/SYS/exe/run/backint`);
	if (not $options{noclean}){
		&logging("deleting LGTO-Files, backint and script from install-path");
		`rm $options{path_inst}/LGTO*`;
		`rm $options{path_inst}/backint`;
		`rm $options{path_inst}/update_aix_clients.pl`;
	}
	`cat $options{logfile}`;
	
}
}


__END__

=pod

=head1 VERSION 1.0

=head1 NAME update_aix_clients.pl

=head1 DESCRIPTION

The script updates networker client on aix and reinstalls modules

=head1 SYNOPSIS

B<update_aix_clients.pl> -inst <install-path>

B<update_aix_clients.pl> -help

=head1 OPTIONS

=over 4

=item -install <install-path>   (path to rte-packages)

=item -max     (flag if system has MaxDB)

=item -noclean (flag if packages and script should not be deleted)

=item -help    displays help

=back

=cut
